<%@ page import="org.apache.commons.lang3.StringEscapeUtils,
                     java.io.IOException,
                     java.io.Writer,
                     java.io.PrintWriter,
                     java.util.*"%><%
%>
<%@include file="/libs/foundation/global.jsp"%><cq:includeClientLib css="jobs" /><%

    String rootPath = properties.get("rootPath", "");
    if (rootPath.length() == 0) {
        rootPath = currentNode.getPath();
    }

    //String sortInfo = properties.get("sortInfo", null);

    String[] cols = new String[]{"JobTitle","Desc","Category","MoreInfo"};
    String[] headers = new String[]{"Job Title","Description","Category","More info"};
    String sortInfo = "JobTitle";

    List<String> propertiesNames = new ArrayList<String>();
    propertiesNames.addAll(Arrays.asList(cols));

    List<String> headersNames = new ArrayList<String>();
    headersNames.addAll(Arrays.asList(headers));

    %><div class="table"><%
    Resource r = slingRequest.getResourceResolver().getResource(rootPath);

        Node rootNode = r.adaptTo(Node.class);
        CareersList pl = new CareersList(rootNode,propertiesNames,sortInfo);
        
        pl.draw(out,propertiesNames,headersNames, request);
    %>

</div>

<%!
    public class CareersList {
        private Set<Career> careers;

        public CareersList (Node rootNode, List<String> propertiesNames, String sortInfo) throws RepositoryException {
            this.careers= new TreeSet<Career>(new CareerComparator(sortInfo));
            

            if(rootNode!=null) {
                NodeIterator children = rootNode.getNodes();

                while (children.hasNext()) {
                    Node node = children.nextNode();
                    if(node != null) {
                        String path = node.getPath();
                        Properties properties = new Properties();
                        for (int i = 0; i < propertiesNames.size(); i++) {
                            String name = propertiesNames.get(i);
                            if(node.hasProperty(name)) {
                                properties.put(name,node.getProperty(name).getString());
                            }
                        }
                        this.careers.add(new Career(path,properties));
                    }
                }
            }
        }

        public void draw(Writer w, List<String> propertiesNames, List<String> headers, HttpServletRequest request) throws IOException {
            PrintWriter out = new PrintWriter(w);
            String url = request.getRequestURL().toString();
            
           // out.print("cat: " + request.getParameter("cat"));
            
            out.print("<div class=\"jobsCR\"><div class=\"topJobs\">Open Positions - Apply Now </div>");

            boolean flag=true;
            String jobs="";
            String info="";
            String cat="";
            for (Career career : careers) {
                flag=!flag;
                info="<div class=\"job "+ ((flag)? "job2" : "") + "\">";
                Properties prop = career.getProperties();
                for (String name : propertiesNames) {
                    String value = (String) prop.get(name);
                    if(value!=null) {

                        if(name.equals("JobTitle"))
                                info=info +"<div class=\"position\">"+StringEscapeUtils.escapeHtml4(value)+"</div>";
                            
                        if(name.equals("Desc"))
                                info=info +"<div class=\"description\">"+StringEscapeUtils.escapeHtml4(value)+"</div>";
                        if(name.equals("Category")){
                            // info=info +"<div class=\"links\">";
                             info=info +"<a href=\"" + url + "?cat=" + java.net.URLEncoder.encode(value, "ISO-8859-1")  + "\" class=\"classname\">Similar Job</a>";
                             info=info +"<a class=\"classname\" href=\"/content/avventa/careers/referralprogram.html\">Refer to a friend</a>";
                            
                             info=info +"<a class=\"classname\" href=\"/content/avventa/careers/jobboard/application.html\">Apply</a>";
                             //info=info +"</div>";
                             cat=StringEscapeUtils.escapeHtml4(value);
                            // out.print("</div><div style=\"clear:both;\">");
                      }
                         if(name.equals("MoreInfo"))
                                 info=info +"<a href=\"" + value + "\" class=\"classname\">More info</a>";
                              
                    } else {
                        out.print("&nbsp;");
                    }
                }
                info= info + "</div>";
                
                if(request.getParameter("cat")==null ){
                    jobs=jobs +info;
                }else{
                
                    if(cat.equals(request.getParameter("cat"))){
                        jobs=jobs +info;
                    }
                }
            }
            out.print(jobs);
            out.print("</div><div class=\"footerJobs\"> </div></div>");
            out.print("<div style=\"clear:both;\"></div>");
        }

        public Set<Career> getCareers() {
            return this.careers;
        }
       
        private class CareerComparator implements Comparator<CareersList.Career> {
            private String sortInfo;
            public CareerComparator(String sortInfo) {
                this.sortInfo = sortInfo;
            }

            public int compare(CareersList.Career o1, CareersList.Career o2) {
                if(this.sortInfo!=null) {
                    String v1 = o1.getProperties().getProperty(this.sortInfo,"");
                    String v2 = o2.getProperties().getProperty(this.sortInfo,"");
                    int cmp = v1.compareTo(v2);
                    if(cmp!=0) {
                        return cmp;
                    }
                }

                return o1.getPath().compareTo(o2.getPath());
            }
        }

        public class Career {
            private String path;
            private Properties properties;

            public Career(String path, Properties properties) {
                this.path = path;
                this.properties = properties;
            }

            public String getPath() {
                return path;
            }

            public void setPath(String path) {
                this.path = path;
            }

            public Properties getProperties() {
                return properties;
            }

            public void setProperties(Properties properties) {
                this.properties = properties;
            }
        }
    }
%>